<?php
include_once("includes/util.php");
include_once("../includes/thumb.php");
include_once("bean/uploadbean.class.php");
include_once("classes/JKUpload.class.php");
class upload 
{
	var $db;
 	var $smarty;
 	var $imgExt;
 	
    function upload($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
		$this->imgExt = "jpg,gif,bmp,png"; //TODO: add more
	}
	
	
	function uploadFile($name, $image, $type)
	{
		$error = -1;
		
		$source = $image;
		if($type == "jpeg" || $type == "jpe" || $type == "jpg")
		{
			$src = imagecreatefromjpeg($source);
		}
		else if($type == "gif")
		{
			$src = imagecreatefromgif($source);
		}
		else if($type == "png")
		{
			$src = imagecreatefrompng($source);
		}
		else if($type == "bmp")
		{
			$src = uploadbean::imagecreatefrombmp($source);
		}
		list($width,$height) = getimagesize($source);
		if($type != "bmp")
		{
			
			if($width > 600 || $height > 500)
			{
				if($width > $height || $width == $height)
				{
					$newwidth_1 = 600;
					$newheight_1=($height/$width)*600;
					if($newheight_1 > 500)
					{
						$newheight = 500;
						$newwidth = ($newwidth_1/$newheight_1)*500;
					}
					else
					{
						$newwidth = $newwidth_1;
						$newheight = $newheight_1;
					}
						
				}
				else
				{
					$newheight = 500;
					$newwidth = ($width/$height)*500;
				}
			}
			else
			{
				$newwidth = $width;
				$newheight = $height;
			}
			$tmp=imagecreatetruecolor($newwidth,$newheight);
			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		}
		else
		{
			$tmp=imagecreatetruecolor($width,$height);
			imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$width,$height);
		}			
		if($type != "bmp")
		{
			$$name = "$name"."_".date("YmdHms").".$type";
			if($_SESSION['mod'] == "partnerman")
			{	
				
				$target = "../".UPLOAD_DIR_STORE."$name"."_".date("YmdHms").".$type";
				$_SESSION['partner'] = array($$name);
			}
			if($_SESSION['mod'] == "news")
			{	
				
				$target = "../".UPLOAD_DIR_STORE."$name"."_".date("YmdHms").".$type";
				$_SESSION['news'] = array($$name);
			}
			else
			{
				$target = "../".UPLOAD_DIR_STORE."$name"."_".date("YmdHms").".$type";
				$_SESSION['product'] = array($$name);
			}
		}
		else
		{
			$$name = $_FILES['userfile']['name'];
			$target = "../".UPLOAD_DIR_STORE.$_FILES['userfile']['name'];
			$_SESSION['product'] = array($$name);
		}
		if($type == "jpeg" || $type == "jpe" || $type == "jpg")
		{
			imagejpeg($tmp, $target);
		}
		else if($type == "gif")
		{
			imagegif($tmp, $target);
		}
		else if($type == "png")
		{
			imagepng($tmp, $target);
		}
		else if($type == "bmp")
		{
			uploadbean::imagebmp($tmp, $target);
		}
							
		imagedestroy($src);
		imagedestroy($tmp);
				
		return $$name;
	}
	
	
	function getUploadImages()
	{
		$arrImgExt =  explode(",", $this->imgExt);
		$d = dir(UPLOAD_DIR.$_SESSION['path']);
		$file_list = array();
		if($d)
		{
			while (false !== ($entry = $d->read())) 
			{
			   if($entry == "." || $entry == "..")	
					continue;
			
			   //$pos = strrchr($entry, '.');
			   //$ext = substr($pos, 1);
			   $pathInfo = pathinfo($entry);
			   
			   
			   //ext is image extension
			   if(in_array(strtolower($pathInfo['extension']), $arrImgExt))
			   {
				   //if(in_array(strtolower($file_list[$idx]['ext']), $arrImgExt))
				   //{
				   		$file_list[$idx]['image'] = "1";
				   //}
					
				   $idx = count($file_list);
				   $file_list[$idx]['name'] = $entry;
				   
				   $pos = strrchr(substr(UPLOAD_DIR.$_SESSION['path'], 0, 
				   				strlen(UPLOAD_DIR.$_SESSION['path']) - 1), '/');
				   $path = substr($pos, 1);
				   
				   $file_list[$idx]['link'] = "../" . $path . "/" . $entry;
			   }
			}

			$d->close();
		}
		
		
		return $file_list;
	}
	
	function dir_walker($sdir)
	{
		$arrImgExt = explode(",", $this->imgExt);
		
		$d = dir($sdir);
		$file_list = array();
		if($d)
		{
			while (false !== ($entry = $d->read())) 
			{
			   if($entry == '.' || $entry == '..' || $entry == 'thumb')	
					continue;
					
				if(is_dir($d->path . $entry))
				{
				   $arrTemp = $this->dir_walker($d->path . $entry);
				   $file_list = array_merge_recursive($file_list , $arrTemp);
				}
				else
				{
					$idx = count($file_list);
				   $file_list[$idx]['name'] = $entry;
				   
				   $file_list[$idx]['link'] = $d->path . '/' . $entry;
				   $file_list[$idx]['type'] = basename($sdir);
				   
				   $pathInfo = pathinfo($entry);
				   
				   $file_list[$idx]['ext'] = $pathInfo['extension'];
				   
				   if(in_array(strtolower($file_list[$idx]['ext']), $arrImgExt))
				   {
				   		$file_list[$idx]['image'] = "1";
				   }
				}
			}

			$d->close();
		}
		
		return $file_list;
	}
	
	function process()
	{
		global $download_folder_cat;
		
		$arrImgExt = explode(",", $this->imgExt);
		
		if(isset($_POST['uploadfile']) && $_POST['uploadfile'] == 'upload')
		{
			if($_FILES['userfile']['name'] != null && $_FILES['userfile']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['userfile']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_files($_FILES['userfile']['tmp_name'], "img", $image_type);
				} 
			}
		}
		else
		{
			if(isset($_GET['del']))
			{
				$file = $_GET['del'];
				//@unlink(UPLOAD_FILES . "thumb/" . $file);
				if(@unlink("../".UPLOAD_FILES . $file))
				{
					$this->smarty->assign("message", sprintf(_("File <b>%s</b> has been deleted."), $file));
				}
			}
			
			if(isset($_POST['changeShowImg']))
			{
				$_SESSION['showImg'] = 0;
				if(isset($_POST['showImg']))
				{
					$_SESSION['showImg'] = "1";
				}
			}
		}
		
		$d = dir(UPLOAD_DIR_FILES);
		$file_list = array();
		if($d)
		{
			while (false !== ($entry = $d->read())) 
			{
			   if($entry == '.' || $entry == '..' || $entry == 'thumb')	
					continue;
					
				if(is_dir($d->path . $entry))
				{
				   $arrTemp = $this->dir_walker($d->path . $entry);
				   $file_list = array_merge_recursive($file_list, $arrTemp);
				}
				else
				{
					$idx = count($file_list);
				   $file_list[$idx]['name'] = $entry;
				   
				   $file_list[$idx]['link'] = FOLDER_HOST.UPLOAD_FILES. $entry;
				   $file_list[$idx]['type'] = '';	//not for download
				   
				   $pathInfo = pathinfo($entry);
				   
				   $file_list[$idx]['ext'] = $pathInfo['extension'];
				   
				   if(in_array(strtolower($file_list[$idx]['ext']), $arrImgExt))
				   {
				   		$file_list[$idx]['image'] = "1";
				   }
				}
			}

			$d->close();
		}

		$this->smarty->assign("FILES", $file_list);
		$this->smarty->assign("FOLDER_CAT", $download_folder_cat);
		
		$this->smarty->display('upload.tpl');
	}
}
?>